const { DataTypes } = require('sequelize');
const bcrypt = require('bcryptjs');
const sequelize = require('../config/database');

const User = sequelize.define('user_messages', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: '用户ID'
  },
  base_name: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '基地名称'
  },
  phone_number: {
    type: DataTypes.STRING(20),
    unique: true,
    allowNull: false,
    comment: '电话（作为账号）'
  },
  password: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: '密码',
    set(value) {
      // 确保这个setter被调用
      console.log('正在设置密码，原始值:', value);
      const salt = bcrypt.genSaltSync(10);
      const hash = bcrypt.hashSync(value, salt);
      this.setDataValue('password', hash);
    }
  },
  real_name: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '真实姓名'
  },
  gender: {
    type: DataTypes.ENUM('男', '女'),
    allowNull: true,
    comment: '性别'
  },
  id_card: {
    type: DataTypes.STRING(18),
    unique: true,
    allowNull: true,
    comment: '身份证号'
  },
  address: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: '家庭地址'
  },
  emergency_contact: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '紧急联系人'
  },
  emergency_relation: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: '紧急联系人关系'
  },
  bank_account_name: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '银行卡用户名'
  },
  bank_account_number: {
    type: DataTypes.STRING(30),
    allowNull: true,
    comment: '银行卡号'
  },
  bank_name: {
    type: DataTypes.STRING(50),
    allowNull: true,
    comment: '所属银行'
  },
  hire_date: {
    type: DataTypes.DATEONLY,
    allowNull: true,
    comment: '入职日期'
  },
  leave_date: {
    type: DataTypes.DATEONLY,
    allowNull: true,
    comment: '离职日期'
  },
  submit_date: {
    type: DataTypes.DATEONLY,
    allowNull: true,
    comment: '提交日期'
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: '是否在职(1:在职,0:离职)'
  },
  management_permission: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: '管理权限(1:有,0:无)'
  },
  base_type: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '基地类型（本部公司，外部公司）'
  }
}, {
  timestamps: true, // 自动管理 createdAt 和 updatedAt
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  paranoid: false, // 不使用软删除（因为已有 is_active 状态）
  comment: '用户信息表',
  indexes: [
    // 唯一约束
    { unique: true, fields: ['phone_number'], name: 'uniq_phone' },
    { unique: true, fields: ['id_card'], name: 'uniq_id_card' },

    // 普通索引
    { fields: ['real_name'], name: 'idx_real_name' },
    { fields: ['is_active'], name: 'idx_is_active' },
    { fields: ['hire_date'], name: 'idx_hire_date' }
  ]
});

// 添加实例方法：验证密码
User.prototype.verifyPassword = function(password) {
  return bcrypt.compareSync(password, this.password);
};

// 安全返回用户信息（排除密码等敏感字段）
User.prototype.toJSON = function() {
  const values = Object.assign({}, this.get());
  delete values.password;
  return values;
};

module.exports = User;